package com.usian.service;

import com.usian.mapper.TbUserMapper;
import com.usian.pojo.TbUser;
import com.usian.pojo.TbUserExample;
import com.usian.redis.RedisClient;
import com.usian.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author lpf
 * @Date 2022/3/26 下午12:18
 * @ProjectName java134-e-commerce-project
 * @Version 1.0
 * @Description:
 */
@Service
@Transactional
public class SSOServiceImpl implements SSOService{

    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private RedisClient redisClient;

    @Value("${SESSION_EXPIRE}")
    private Long SESSION_EXPIRE;

    /**
     * 实现检验用户名和手机号是否被注册功能
     * @param checkValue
     * @param checkFlag
     * @return
     * @Auth lpf
     */
    @Override
    public Boolean checkUserInfo(String checkValue, Integer checkFlag) {
        TbUserExample tbUserExample = new TbUserExample();
        TbUserExample.Criteria criteria = tbUserExample.createCriteria();
        if (checkFlag == 1){
            criteria.andUsernameEqualTo(checkValue);
        } else if (checkFlag == 2){
            criteria.andPhoneEqualTo(checkValue);
        }
        List<TbUser> tbUsers = tbUserMapper.selectByExample(tbUserExample);
        if (tbUsers != null && tbUsers.size() > 0){
            return false;
        }
        return true;
    }

    /**
     * 实现用户注册功能
     * @param tbUser
     * @return
     * @Auth lpf
     */
    @Override
    public Integer userRegister(TbUser tbUser) {
        //md5加密
        String pwd = tbUser.getPassword();
        tbUser.setPassword(MD5Utils.digest(pwd));
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        return tbUserMapper.insertSelective(tbUser);
    }

    /**
     * 用户登录，并使用token来标记用户的登录信息
     * @param username
     * @param password
     * @return
     * @Auth lpf
     */
    @Override
    public Map userLogin(String username, String password) {
        //帐号密码验证
        String pwd = MD5Utils.digest(password);
        TbUserExample tbUserExample = new TbUserExample();
        TbUserExample.Criteria criteria = tbUserExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(pwd);
        List<TbUser> tbUsers = tbUserMapper.selectByExample(tbUserExample);
        if (tbUsers == null || tbUsers.size() == 0){
            return null;
        }
        //生成的随机uuid与user关联，存入redis中
        TbUser tbUser = tbUsers.get(0);
        tbUser.setPassword(null);   //设置密码为空存入redis中，防止密码泄漏
        String token = UUID.randomUUID().toString();
        String key = "USER_INFO:" + token;
        redisClient.set(key, tbUser);
        redisClient.expire(key, SESSION_EXPIRE);  //设置过期时间为一周
        //返回具体数据
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("token", token); //其他模块使用这个token来检查用户是否登录
        resultMap.put("userid", tbUser.getId().toString());
        resultMap.put("username", tbUser.getUsername());
        return resultMap;
    }

    /**
     * 通过token获取登录用户
     * @param token
     * @return
     * @Auth lpf
     */
    @Override
    public TbUser getUserByToken(String token) {
        //查询redis，是否有这个token
        String key = "USER_INFO:" + token;
        TbUser tbUser = (TbUser) redisClient.get(key);
        if (tbUser != null){
            // 重置失效时间
            redisClient.expire(key, SESSION_EXPIRE);
            return tbUser;
        }
        return null;
    }

    /**
     * 用户注销登录功能的实现
     * @param token
     * @return
     * @Auth lpf
     */
    @Override
    public Boolean logout(String token) {
        String key = "USER_INFO:" + token;
        return redisClient.del(key);
    }
}
